/**
 * Copyright 2011 Google
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.appengine.codelab;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

/**
 * This class defines the methods for basic operations of create, update & retrieve
 * for customer entity
 * 
 * @author 
 *
 */
public class User {

	/**
	 * Checks if the entity is existing and if it is not, it creates the entity
	 * else it updates the entity
	 * 
	 * @param nickName
	 *          : username for the user
	 * @param geo
	 *          : geo address of customer
	 * @param boxid
	 *          : boxid number
	 * @param language
	 *          : language address of customer
	 * @param country
	 *          : country number
	 */
  public static void createOrUpdateUser(String nickName, String geo, String boxid, String language, String country) {
    Entity user = getSingleUser(nickName);
    if (user == null) {
    	user = new Entity("User", nickName);
    	user.setProperty("name", nickName);
    	user.setProperty("boxid", boxid);
    	user.setProperty("geo", geo);
    	user.setProperty("language", language);
    	user.setProperty("country", country);
    	user.setProperty("lastVisited", "now");
    } else {
      if (geo != null && !"".equals(geo)) {
        user.setProperty("geo", geo);
      }
      if (boxid != null && !"".equals(boxid)) {
        user.setProperty("boxid", boxid);
      }
      user.setProperty("lastVisited", "now");
    }
    Util.persistEntity(user);
  }
  
	/**
	 * Updates the User entity's geo
	 * 
	 * @param nickName
	 *          : username for the user
	 * @param geo
	 *          : geo address of customer
	 */
  public static void updateUserGeo(String nickName, String geo) {
  Entity user = getSingleUser(nickName);
  if (user == null) {
  	user = new Entity("User", nickName);
  	user.setProperty("geo", geo);
  } else {
    if (geo != null && !"".equals(geo)) {
      user.setProperty("geo", geo);
    }
  }
  Util.persistEntity(user);
}

	/**
	 * List all the customers available
	 * 
	 * @return an iterable list with all the customers
	 */
  public static Iterable<Entity> getAllUsers() {
    Iterable<Entity> entities = Util.listEntities("User", null, null);
    return entities;
  }

	/**
	 * Searches for an user and returns the entity as an iterable The search is
	 * performed by creating a query and searching for the attribute
	 * 
	 * @param userName
	 *          : username of the customer
	 * @return iterable with the users searched for
	 */
  public static Iterable<Entity> getUser(String userName) {
    Iterable<Entity> entities = Util.listEntities("User", "name",	userName);
    return entities;
  }

	/**
	 * Searches for a user and returns the entity as an iterable The search is
	 * key based instead of query
	 * 
	 * @param userrName
	 *          : username of the customer
	 * @return the entity with the username as key
	 */
  public static Entity getSingleUser(String userName) {
    Key key = KeyFactory.createKey("User", userName);
    return Util.findEntity(key);
  }
}
